Skip to content

load_it: fix unsigned underflow in IT pattern command bounds check - #112

Closed
ojaneri wants to merge 1 commit into
Konstanty:masterfrom
ojaneri:fix-it-pattern-oob-underflow
Closed

ojaneri wants to merge 1 commit into
Konstanty:masterfrom
ojaneri:fix-it-pattern-oob-underflow

Conversation

@ojaneri

@ojaneri ojaneri commented Nov 15, 2026

Copy link
Copy Markdown

The IT pattern reader's two-byte command/param read is guarded by
if (i > len - 2) break; (src/load_it.cpp). len is a UINT read from
the file via READ_LE16 (0..65535) and can be 0 or 1. When len < 2,
len - 2 underflows to a huge unsigned value, so the check never breaks and
the following p[i++] / p[i++] read past the end of the pattern data (and,
near the buffer end, past the module buffer).

Every other read in the same loop uses if (i >= len) break; — this is the one
site that doesn't. A crafted .it file with a pattern length < 2 and a channel
carrying the command mask bit reaches it.

The fix uses an addition that can't underflow, matching the intent (two bytes
must be available):

-				if (i > len - 2) break;
+				if (i + 1 >= len) break;

Still present on master and the oob_read_fixes branch — it wasn't covered by
the recent IT/OOB hardening, so sending it along.

The two-byte command/param read in the IT pattern loop is guarded by
`if (i > len - 2) break;`. len is a UINT read from the file (READ_LE16,
0..65535) and can be 0 or 1; then len - 2 underflows to a huge unsigned
value, the check never fires, and the following p[i++]/p[i++] read past
the end of the pattern data. Every other read in the loop uses the safe
`if (i >= len) break;` form. Use an addition that cannot underflow.
@Konstanty

Copy link
Copy Markdown
Owner

The existing code doesn't OOB read, for len == 0, L 468 prevents len = 0, and for len=1, L 480 will break.

@ojaneri

ojaneri commented Nov 15, 2026 via email

Copy link
Copy Markdown
Author

@ojaneri

ojaneri commented Nov 15, 2026

Copy link
Copy Markdown
Author

Yeah, you're right. Traced it again and it can't reach that read with len < 2.

len==0 breaks at L468 before any read. For len==1, hitting the chnmask[ch] & 8 branch needs bit 8 already set, but chnmask is memset to 0 and only gets assigned at L481, which is behind the L480 i >= len break (fires at i==1==len). So it never gets there. len>=2 makes len-2 fine anyway.

So no OOB, the underflow's dead code. Closing this. The i+1 >= len form only buys you avoiding the underflow for readability, not worth keeping if you'd rather not. Thanks for looking.

@ojaneri ojaneri closed this Nov 15, 2026
@Konstanty

Copy link
Copy Markdown
Owner

It could be good to clean up the code for readability / prevent future flagging. Just not marking it as an OOB read

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants